// pages/securityCenter/amendPhone/amendPhone.js
var util = require('../../../utils/util.js')
var app = getApp()
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  phonenum: "", //新手机号
	  newphonenum: "", //新手机号
	  sendmsgclassone: "", //验证码按钮class
	  sendmsgclasstwo: "", //验证码按钮class
	  getmsgone: "获取验证码", //验证码按钮标题
	  getmsgtwo: "获取验证码", //验证码按钮标题
	  verifcodeone: null,	//服务器验证码
	  verifcodetwo: null,	//服务器验证码
	  sendtpone: true,	//发送状态
	  sendtptwo: true,	//发送状态
	  submitload: false,	//提交Loading状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()
		//初始发送短信请求
	  // this.getVerifCodeOne()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  this.setData({ phonenum : app.globalData.userPhone})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  //新手机号绑定事件 newphonenum
  newphonenumbindtp: function (e) {
	  this.setData({ newphonenum: e.detail.value })
  },
  formSubmit: function (e) {
	  var warn = "";
	  var that = this;

	  var FirstCode = e.detail.value.FirstCode
	  var NewPhoneNumber = e.detail.value.NewPhoneNumber
	  var TwoCode = e.detail.value.TwoCode
	  if (FirstCode == "") {
		  warn = "请填写原手机号验证码！";
	  } else if (FirstCode != that.data.verifcodeone) {
		  warn = "原手机号验证码错误！";
	  } else if (NewPhoneNumber == "") {
		  warn = "请填写您的手机号！";
	  } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(NewPhoneNumber))) {
		  warn = "手机号格式不正确";
    } else if (NewPhoneNumber == app.globalData.userPhone) {
      warn = "请填写新手机号验证码！";
    } else if (TwoCode == "") {
		  warn = "请填写新手机号验证码！";
	  } else if (TwoCode != that.data.verifcodetwo) {
		  warn = "新手机号验证码错误！";
	  } else {
		  flag = true;
	  }

	  //执行结果
	  if (flag) {
		  //开启加载
		  that.setData({ submitload: true })
		  	//服务器请求，更换手机号
		  util.AJAX('Home/Login/upPhoneNum', { userinfoid: app.globalData.userInfoId,newphonenum: NewPhoneNumber }, function (res) { //方法体
			  //关闭加载
			  that.setData({ submitload: false })

			  if (res.data.StatusCode == 100) {  //更新成功
				  wx.showToast({
					  title: res.data.Msg,
					  icon: 'success',
					  duration: 2000
				  })

				  setTimeout(function () {
					  //退出登录状态
					  app.logout()
					  //回退两层
					  wx.navigateBack({
						  delta: 2
					  })

					  app.navigateTologing()  //跳转到登录
					  
				  }, 2000);
			  } else {	//更新失败
				  wx.showModal({
					  title: '提示',
					  content: res.data.Msg
				  })
			  }
		  }, 'POST')
	  } else {
		  wx.showModal({
			  title: '提示',
			  content: warn
		  })
	  }
  },
  //获取手机验证码
  getVerifCodeOne: function (e) {
	  var that = this
	  var timer = that.data.sendtpone
	  if (timer) {
		  var telPhone = app.globalData.userPhone
		  //验证手机是否为空
		  if (telPhone == "") {
			  wx.showModal({
				  title: '提示',
				  content: "参数错误，请稍后重试！"
			  })
		  } else {
			  var time = 120  //倒计时总时间
			  that.setData({
				  sendmsgclassone: "active",
				  getmsgone: "发送中...",
				  sendtpone: false
			  })
			  
			  //去服务端发送短信验证码
			  util.AJAX('Home/Login/sendPhoneCode', { mobile: telPhone }, function (res) { //方法体
				  if (res.data.status == 100) {  //发送成功
					  //锁定手机号
					  //更改验证码值
					  that.setData({
						  verifcodeone: res.data.verify
					  })
					  //开始计时
					  var inter = setInterval(function () {
						  that.setData({
							  sendmsgclassone: "active",
							  getmsgone: time + "s",
						  })
						  time--
						  if (time < 0) {
							  clearInterval(inter)
							  that.setData({
								  sendmsgclassone: "",
								  getmsgone: "重新获取",
								  sendtpone: true
							  })
						  }
					  }, 1000)
				  } else {	//发送失败
					  that.setData({
						  sendmsgclassone: "",
						  getmsgone: "重新获取",
						  sendtpone: true
					  })
				  }
			  }, 'POST')
		  }

	  }
  },
  //获取手机验证码
  getVerifCodeTwo: function (e) {
	  var warn = "";
	  var that = this
	  var timer = that.data.sendtptwo
	  if (timer) {
		  var telPhone = that.data.newphonenum
		  //验证手机是否为空
		  if (telPhone == "") {
			  wx.showModal({
				  title: '提示',
				  content: "请填写新手机号！"
        })
		  } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(telPhone))) {
			  wx.showModal({
				  title: '提示',
				  content: "新手机号格式不正确"
			  })
      } else if (app.globalData.userPhone == telPhone) {
        wx.showModal({
          title: '提示',
          content: "新手机号与原手机号相同了"
        })
      } else {
			  var time = 120  //倒计时总时间
			  that.setData({
				  sendmsgclasstwo: "active",
				  getmsgtwo: "发送中...",
				  sendtptwo: false
			  })

			  //去服务端发送短信验证码
			  util.AJAX('Home/Login/sendPhoneCode', { mobile: telPhone }, function (res) { //方法体
				  if (res.data.status == 100) {  //发送成功
					  //锁定手机号
					  //更改验证码值
					  that.setData({
						  verifcodetwo: res.data.verify
					  })
					  //开始计时
					  var inter = setInterval(function () {
						  that.setData({
							  sendmsgclasstwo: "active",
							  getmsgtwo: time + "s",
						  })
						  time--
						  if (time < 0) {
							  clearInterval(inter)
							  that.setData({
								  sendmsgclasstwo: "",
								  getmsgtwo: "获取验证码",
								  sendtptwo: true
							  })
						  }
					  }, 1000)
				  } else {	//发送失败
					  that.setData({
						  sendmsgclasstwo: "",
						  getmsgtwo: "获取验证码",
						  sendtptwo: true
					  })
				  }
			  }, 'POST')
		  }

	  }
   }
})